//
//  WBNetwork.swift
//  weiboswift
//
//  Created by dingzhibing on 2017/3/18.
//  Copyright © 2017年 dingzhibing. All rights reserved.
//

import Foundation

//MARK: - 封装新浪微博的网络请求方法
extension WBNetworkManager{
    /// <#Description#>
    ///
    /// - Parameters:
    ///   - since_id: 返回id比since_id大的微博 默认为0
    ///   - max_id: 返回id比since_id小的微博
    ///   - completion: <#completion description#>
    func statusList(sinceId:Int64 = 0, maxId:Int64 = 0, completion:@escaping (_ list:[[String:AnyObject]]?,_ isSuccess:Bool)->()){
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        //let params = ["access_token":"2.004YLCCG0yLHb8f708bb6eb2ZGo1MC"]
        
        let params = ["since_id":"\(sinceId)",
                      "max_id":"\(maxId)"]
        
        tokenRequest(URLString: urlStr, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            //print(json ?? "no data")
            
            let result = (json as AnyObject)["statuses"] as? [[String:AnyObject]]
            print(result ?? []);
            completion(result,isSuccess)
        }
    }
    
    //返回微博的未读数量
    func unreadCount(completion:@escaping (_ count:Int)->Void){
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        guard let uid = userAccount.uid else {
            return
        }
        let params = ["uid":uid]
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            let dict = json as? [String:AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
        }
    }
}

extension WBNetworkManager{
    //加载用户信息
    func loadUserInfo(completion:@escaping (_ dict:[String:AnyObject])->()){
        guard let uid = userAccount.uid else {
            return
        }
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid":uid]
        
        //发起网络请求
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            //print(json ?? "")
            //完成回调
            completion((json as? [String:AnyObject]) ?? [:])
        }
    }
}

extension WBNetworkManager {
    
    ///加载 accesstoken
    func loadAccessToken(code:String,completion:@escaping (_ isSuccess:Bool)->())  {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":WBAppKey,
                      "client_secret":WBAppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WBRedirectURI]
        
        request(method: .POST, URLString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            print(json ?? "")
            self.userAccount.yy_modelSet(with: json as? [String : AnyObject] ?? [:])
            print(self.userAccount)

            //加载当前用户信息
            self.loadUserInfo(completion: { (dict) in
                print(dict)
                //设置昵称和头像地址
                self.userAccount.yy_modelSet(with: dict)
                self.userAccount.saveAccount()
                //完成回调
                completion(isSuccess)
            })
            
            
        }
    }
}
